package com.cloud2022.sharding.config.CustomerScancodeRecordRule;

import com.cloud2022.sharding.config.CodeShardingMap;
import org.apache.shardingsphere.api.sharding.complex.ComplexKeysShardingAlgorithm;
import org.apache.shardingsphere.api.sharding.complex.ComplexKeysShardingValue;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 等值查询使用的分片算法，包括in
 * @author libin
 */
@Component
public class CustomerScancodeRecordRule implements ComplexKeysShardingAlgorithm<String> {

    @Override
    public Collection<String> doSharding(Collection<String> availableTargetNames, ComplexKeysShardingValue<String> shardingValue) {
        Map<String, Collection<String>> columnNameAndShardingValuesMap = shardingValue.getColumnNameAndShardingValuesMap();
        List<String> queryList = (List<String>) columnNameAndShardingValuesMap.get("_qrcode_a");
        String query = queryList.get(0);
        String logicTableName = shardingValue.getLogicTableName();

        String shardingKey2 = query.substring(query.length()-1);
        String key2 = CodeShardingMap.map.get(shardingKey2);

        List<String> tables = new ArrayList<>(1);
        tables.add("_"+key2+logicTableName);
        return tables;
    }

}
